import os.path

import requests

imgage_url = 'https://img2.baidu.com/it/u=3528066264,854193107&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=666'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'}
# content是获取bytes数据类型
html = requests.get(url=imgage_url, headers=headers).content
directory = './images/'
# 判断目录是否存在，不存在创建
if not os.path.exists(directory):
    os.makedirs(directory)
filename = directory + imgage_url[-10:]+'.jpg'
with open(filename, 'wb') as f:
    f.write(html)

